<template>
  <div>
    <crumbs :BreadCrumb="BreadCrumb"></crumbs>
    <el-form
        ref="formline"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
        label-width="90px"
    >
      <el-form-item label="合同编码">
        <el-input v-model="formInline.cnum" clearable></el-input>
      </el-form-item>
      <el-form-item label="合同名称">
        <el-input v-model="formInline.cname" clearable></el-input>
      </el-form-item>
      <el-form-item label="单位名称">
        <el-input v-model="formInline.enterpriseName" clearable></el-input>
      </el-form-item>
      <el-form-item label="单位负责人">
        <el-input v-model="formInline.enterpriseLead" clearable></el-input>
      </el-form-item>
       <el-form-item label="销售人员">
        <el-input v-model="formInline.personName" clearable></el-input>
      </el-form-item>
      <el-form-item label="合同状态">
        <el-select v-model="formInline.status" clearable>
          <el-option v-for="(v, i) in contractStatus" v-if="v.value !== 4" :key="i" :label="v.label" :value="v.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label-width="110px" label="签订日期">
        <el-date-picker
          v-model="orderDateRange"
          type="daterange"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
          >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="getlist"></el-button>
      </el-form-item>
    </el-form>
    <el-table
        ref="multipleTable"
        :data="tableDatalist"
        tooltip-effect="dark"
        style="width: 100%"
    >
      <!-- <el-table-column type="selection" width="55"> </el-table-column> -->
      <!-- <el-table-column sortable  prop="orderNo" label="体检单号" width="120">
      </el-table-column> -->
      <el-table-column prop="enterpriseName" label="单位名称" width="120">
      </el-table-column>
      <el-table-column prop="cname" label="合同名称" width="200">
      </el-table-column>
      <el-table-column prop="cnum" label="合同编号" width="140">
      </el-table-column>
      <el-table-column prop="medicalType" label="体检类型" width="120">
        <template slot-scope="scope">{{ formatType(scope.row.medicalType) }}</template>
      </el-table-column>
      <el-table-column label="签订日期" width="150">
        <template slot-scope="scope">
          {{ scope.row.orderDate|formatDate }}
        </template>
      </el-table-column>
      <el-table-column label="有效日期" width="150">
        <template slot-scope="scope">
          {{ scope.row.endDate|formatDate }}
        </template>
      </el-table-column>
      <el-table-column prop="enterpriseLead" label="单位负责人" width="120">
      </el-table-column>
      <el-table-column prop="enterpriseTel" label="联系方式" width="120">
      </el-table-column>
      <el-table-column prop="personName" label="销售人员" width="120">
      </el-table-column>
      <el-table-column prop="discount" label="折扣率" width="120">
      </el-table-column>
      <el-table-column label="合同状态" width="120">
        <template slot-scope="scope">
          {{ scope.row.status| formatContractStatus }}
        </template>
      </el-table-column>
      <el-table-column prop="memberCount" label="成员人数" width="120">
      </el-table-column>
      <el-table-column prop="checkCount" label="登记人数" width="120">
      </el-table-column>
      <el-table-column prop="editor" label="编辑人" width="120">
      </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button @click="close(scope.row)" type="primary" size="small">合同封闭</el-button>
          <el-button v-if="scope.row.status!==3" @click="back(scope.row)" type="danger" size="small">撤销</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNo"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </div>
</template>

<script lang="ts">
import {formatTimeToStr} from "@src/common/utils/date";
import crumbs from "../../views/Crumbs.vue";
import { contractStatus} from "@src/common/utils/constant";


export default {

  components: {
    crumbs,
  },
  data() {
    return {
      dict:{},
      BreadCrumb: [
        {path: "", name: "检后管理"},
        {path: "/front/contract", name: "合同封闭"},
      ],
      contractStatus:contractStatus,
      formInline:{},
      orderDateRange:[
        formatTimeToStr(new Date(new Date().setMonth(new Date().getMonth() - 1)), "yyyy-MM-dd"),
        formatTimeToStr(new Date(), "yyyy-MM-dd")
      ],
      tableDatalist:[],
      pageNo:0,
      pageSize:0,
      total:0,
    }
  },
  filters:{
    formatDate: function (time) {
      if (time != null && time !== "") {
        var date = new Date(time);
        return formatTimeToStr(date, "yyyy-MM-dd");
      } else {
        return "";
      }
    },
    formatContractStatus:function (val){
      var dict={}
      contractStatus.forEach(v=>{
        dict[v.value] =v.label
      })
      return dict[val]
    }
  },
  created() {
    this.getlist()
    this.searchDictList()
  },
  methods: {
    searchDictList() {
      this.$axios.searchDictList({codes: ["tyType"],}).then((res) => {
        if (res.code === "00000") {
          res.data.forEach(v => {
            this.dict[v.dictCode] = v.items
          })
        }
      });
    },
    formatType(val){
      const map = {}
      this.dict.tyType.forEach(v=>{
        map[v.itemCode] = v.itemName
      })
      if(map[val] !== undefined){
        return map[val]
      }
      return val
    },
    doGetList(req){
      this.$axios.getPeEnterpriseContractsList({
        ...req
      }).then(res => {
        if (res.code == '00000') {
          this.tableDatalist = res.data.records;
          this.total = res.data.total;
        }
        this.form = {};
      })
    },

    getlist() {
      if (this.orderDateRange && this.orderDateRange.length === 2) {
        this.formInline['orderStartDate'] = this.orderDateRange[0]
        this.formInline['orderEndDate'] = this.orderDateRange[1]
      }else {
        delete this.formInline.orderStartDate
        delete this.formInline.orderEndDate
      }
      this.doGetList(this.formInline)
    },
    handleSizeChange(val) {
      this.pageSize = val
      const req = {
      	...this.searchForm,
				pageSize:this.pageSize,
				pageNo:this.pageNo,
      }
      this.doGetList(req)
    },
    // 第几页
    handleCurrentChange(val) {
      this.pageNo = val
      const req = {
      	...this.searchForm,
				pageSize:this.pageSize,
				pageNo:this.pageNo,
      }
      this.doGetList(req)
    },

    close(row){
      this.$confirm('您确定要对此合同进行封口吗？一旦封口，意为合同执行完成，将不能再操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.updatePeEnterpriseContracts({
          ...row,status:2,
        }).then(res => {
          if (res.code == '00000') {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getlist({"contractId": this.id});
          }
        })
      }).catch(() => {
      });
    },
    back(row){
      this.$confirm('您确定要进行撤销吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.updatePeEnterpriseContracts({
          ...row, status:3
        }).then(res => {
          if (res.code == '00000') {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getlist({"contractId": this.id});
          }
        })
      }).catch(() => {
      });
    },

  },
}
</script>

<style>

</style>
